import { dialog, ipcMain } from 'electron'
import type { IpcMainEvent } from 'electron'
import { downloadFile } from '../utils'

ipcMain.on('downloadImage', async (_event: IpcMainEvent, url: string) => {
  const fileName = url.split('/').pop()!

  const { filePath, canceled } = await dialog.showSaveDialog({
    title: '下载图片',
    message: '桌面软件',
    defaultPath: fileName,
    properties: ['createDirectory']
  })

  if (!canceled) {
    downloadFile(url, filePath)
  }
})
